<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$this->model('member')->checklogin();
$return=array();
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('basic');
	$return['list']=array();
	set_html('title','商品评论');
	exi($return);
}
if($_RQ['op']=='commentorder'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->session('goodsid',$id);
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$order['cretetime']=date('Y-m-d h:i:s',$order['cretetime']);                                           //时间格式化
	$order['paytime']=date('Y-m-d h:i:s',$order['paytime']);
	$order['sendtime']=date('Y-m-d h:i:s',$order['sendtime']);
	$order_goods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));   
    if($order_goods){
		foreach($order_goods as $k=>&$item){
			$goodinfo=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']),array('title','image','price'));          //商品信息
			$item['title'] =  $goodinfo['title'];
			$item['image'] =  $goodinfo['image'];
			$item['price'] =  $goodinfo['price'];
			if($item['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$optionid),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$item['optiontitle']=implode("，",$specs);
			}	
		}
	}
	$pingjia = pdo_get('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id'],'commentid'=>0));
	if($pingjia['createtime']){
		$pingjia['endpingjiatime'] = $pingjia['createtime'] + 7*24*3600;
		if($pingjia['endpingjiatime']>TIMESTAMP){
				$pingjia['pingjia']=1;//1修改评价；2追加评价
		}else{
				$zuipingjia = pdo_count('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id'],'commentid >'=>0),'id');
				if($zuipingjia<1){
					$pingjia['commentid']=$pingjia['id'];
					$pingjia['pingjia']=2;
				}
		}
		$pingjia['endpingjiatime']=date('Y-m-d h:i:s',$pingjia['endpingjiatime']);
		$pingjia['images']=json_decode($pingjia['images'],true);
		$return['post']=$pingjia;
	}else{
		$return['post']['star']=0;
		$return['post']['orderid']=$order['id'];
	}
	$goods_count=count($order_goods);
	$shop_setting=$this->plugin_setting('shop');
	$return['order']=$order;
	$return['goods']=$order_goods;
	$return['shop_setting']=$shop_setting;
	$return['goods_count']=$goods_count;
	exi($return);
}
if($_RQ['op']=='comment'){
	if(!$_RQ['id']){
		exi('该商品不存在，请重新选择','error');
	}
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));          //商品信息
	$shop_setting=$this->plugin_setting('shop');
	$this->session('goodsid',$_RQ['id']);
	$return['goods']=$goods;
	$return['shop_setting']=$shop_setting;
	$return['post']['star']=0;
	exi($return);
}
if($_RQ['op']=='submit'){
	$postdata=get_postdata();
	$member=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if(!$postdata['id']){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'goodsid'=>$this->session('goodsid'),
			'orderid'=>intval($postdata['orderid']),
			'content'=>$postdata['content'],
			'images'=>ijson_encode($postdata['images']),
			'star'=>intval($postdata['star']),
			'nickname'=>$member['nickname'],
			'logo'=>$member['avatar'],
			'commentid'=>intval($postdata['commentid']),
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_weishop_goods_comments',$insert);
		exi('提交成功','','goback');
	}else{
		$update=array(
			'content'=>$postdata['content'],
			'images'=>ijson_encode($postdata['images']),
			'star'=>intval($postdata['star']),
		);
		pdo_update('jy_weishop_goods_comments',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$postdata['id']));
		exi('修改成功','','goback');
	}
	
}
?>